package com.luckku.boot.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luckku.boot.common.api.ApiResult;
import com.luckku.boot.entity.Permission;
import com.luckku.boot.entity.Role;
import com.luckku.boot.service.impl.PermissionServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 权限菜单表 前端控制器
 * </p>
 *
 * @author Luckku
 * @since 2022-11-08
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Resource
    private PermissionServiceImpl permissionService;


    @PostMapping
    public ApiResult<?> save(@RequestBody Permission permission) {
        return ApiResult.success(permissionService.save(permission));
    }

    @PutMapping
    public ApiResult<?> update(@RequestBody Permission permission) {
        return ApiResult.success(permissionService.updateById(permission));
    }

    @DeleteMapping("/{id}")
    public ApiResult<?> delete(@PathVariable Long id) {
        Permission permission = permissionService.getById(id);
        permissionService.delete(id);
        return ApiResult.success();
    }

    @GetMapping("/{id}")
    public ApiResult<Permission> findById(@PathVariable Long id) {
        return ApiResult.success(permissionService.getById(id));
    }

    @GetMapping
    public ApiResult<List<Permission>> findAll() {
        return ApiResult.success(permissionService.list());
    }

    @GetMapping("/page")
    public ApiResult<IPage<Permission>> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                                 @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return ApiResult.success(permissionService.page(new Page<>(pageNum, pageSize), Wrappers.<Permission>lambdaQuery().like(Permission::getName, name)));
    }

    @PostMapping("/getByRoles")
    public ApiResult<List<Permission>> getByRoles(@RequestBody List<Role> roles) {
        return ApiResult.success(permissionService.getByRoles(roles));
    }
}

